package com.dwms.controller;

import com.dwms.entity.Request;
import com.dwms.entity.Result;
import com.dwms.entity.system.login.request.LoginRequest;
import com.dwms.entity.system.login.response.VerifyCodeResponse;
import com.dwms.entity.system.menu.response.RouterResponse;
import com.dwms.entity.system.user.response.UserInfo;
import com.dwms.feign.system.LoginFeignClient;
import com.dwms.rsa.RSAService;
import com.dwms.utils.EmptyUtils;
import com.dwms.utils.WebUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @ClassName LoginController
 * @Description: 登录Controller
 * @Create 2021/12/12 17:40
 * @Author WH
 * @Since 1.0.0
 */
@Tag(name = "登录管理")
@RestController(value = "LoginController")
@RequestMapping(value = "/auth")
@RequiredArgsConstructor
public class LoginController {

    private final HttpServletRequest request;
    private final LoginFeignClient loginFeignClient;
    private final RSAService rsaService;

    @Operation(summary= "获取验证码")
    @PostMapping("/getVerifyCode")
    public Result<VerifyCodeResponse> getVerifyCode() {
        return this.loginFeignClient.getVerifyCode();
    }

    @Operation(summary= "获取公钥")
    @PostMapping("/getPublicKey")
    public Map<String, String> getPublicKey() {
        return rsaService.getPublicKey(request.getSession().getId());
    }

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result submit(@RequestBody LoginRequest loginRequest) {
        WebUtil.getAdminRequest(request,loginRequest);
        String password = rsaService.decrypt(loginRequest.getPassword(), loginRequest.getSessionGenerateId());
        loginRequest.setPassword(password);
        return this.loginFeignClient.submit(loginRequest);
    }

    @Operation(summary = "查询用户信息")
    @PostMapping("/getUserInfo")
    public Result<UserInfo> getUserInfo() {
        Request requestVO = new Request();
        WebUtil.getAdminRequest(request,requestVO);
        return this.loginFeignClient.getUserInfo(requestVO);
    }

    @Operation(summary = "获取路由信息")
    @PostMapping("/findRouters")
    public Result<List<RouterResponse>> findRouters() {
        Request requestVO = new Request();
        WebUtil.getAdminRequest(request,requestVO);
        return this.loginFeignClient.findRouters(requestVO);
    }

    @Operation(summary = "登录注销")
    @PostMapping("/logout")
    public Result logout() {
        Request requestVO = new Request();
        WebUtil.getAdminRequest(request,requestVO);
        return loginFeignClient.logout(requestVO);
    }
}
